<?php
/**
 * Object Relational Persistence Mapping Class for table: pay_payment
 *
 * @package	com.athena
 * @author	LHV
 */
class PayPayment extends ORDataObject {

	/**#@+
	 * Fields of table: pay_payment mapped to class members
	 */
	var $pay_payment_id        = '';
	var $ticket_id		       = '';
	var $patient_id		       = '';
	var $department_id         = '';
	var $original_price        = '';
	var $insurance_price       = '';
	var $discount              = '';
	var $patient_price         = '';
	var $real_payment          = '';
	var $enum_patient_type     = '';
	var $enum_examination_type = '';
	/**#@-*/


	/**
	 * DB Table
	 */
	var $_table = 'pay_payment';

	/**
	 * Primary Key
	 */
	var $_key = 'pay_payment_id';

	/**
	 * Internal Name
	 */
	var $_internalName = 'PayPayment';

	/**
	 * Handle instantiation
	 */
	function PayPayment() {
		parent::ORDataObject();
	}

	/**
	 * Set data to the object from a BillStorage object.
	 *
	 * @param BillStorage $objStorage BillStorage object
	 */
    public function setDataFromBillStorage($objStorage) {
        $this->ticket_id                =   $objStorage->getTicketId();
        $this->enum_examination_type    =   $objStorage->getExaminationType();
        $this->enum_patient_type        =   $objStorage->getPatientType();
    }

    public static function getPaymentIdByTicket($ticketId) {
    	$sql   =   "SELECT pay_payment_id
    	   FROM pay_payment
    	   WHERE ticket_id = '$ticketId'";
    	$db = Celini::dbInstance();
    	$res = $db->Execute($sql);
    	if ($res && !$res->EOF) {
    		return $res->fields["pay_payment_id"];
    	}
    	
    	return false;
    }

    public static function getObjectByTicket($ticketId) {
        $paymentId  =   self::getPaymentIdByTicket($ticketId);
        return Celini::newORDO('PayPayment', $paymentId);
    }

    public function getPaymentInfoByTicket($ticketId) {
    	$sql   =   array(
    	   'cols'  =>  "patient_price, real_payment, (patient_price - real_payment) as remaining_amount",
    	   'from'  =>  "$this->_table",
    	   'where' =>  "ticket_id='$ticketId'"
    	);
        $ds    =   new Datasource_sql();
        $ds->setup($this->_db, $sql);
        $res    = $ds->toArray();
        return $res[0];
    }

    public function setData($ticketId, $patientId, $departmentId, $patientType, $examinationType) {
    	$data	=	array(
    		'ticket_id'			=>	$ticketId,
    		'patient_id'		=>	$patientId,
    		'department_id'		=>	$departmentId,
    		'enum_patient_type'	=>	$patientType,
    		'enum_examination_type'	=>	$examinationType
    	);
    	$this->populateArray($data);
    }

    public function getPatientPrice() {
    	return $this->patient_price;
    }

    public function getRealPayment() {
    	return $this->real_payment;
    }

    public function updatePrices($paymentId) {
        $sql  =   "UPDATE pay_payment pp
                  SET pp.patient_price = (SELECT SUM(patient_price) FROM pay_payment_item ppi WHERE ppi.pay_payment_id='$paymentId'),
	                  pp.original_price = (SELECT SUM(original_price) FROM pay_payment_item ppi WHERE ppi.pay_payment_id='$paymentId'),
	                  pp.insurance_price = (SELECT SUM(insurance_price) FROM pay_payment_item ppi WHERE ppi.pay_payment_id='$paymentId'),
	                  pp.discount = (SELECT SUM(discount) FROM pay_payment_item ppi WHERE ppi.pay_payment_id='$paymentId')
                  WHERE pp.pay_payment_id = '$paymentId'";
        $this->_execute($sql);
    }

   public function createTicketPayment($ticketId) {
        $objTicket =   Celini::newORDO('Ticket', $ticketId);
        $objPayment =   Celini::newORDO('PayPayment');
        $objPayment->setData($objTicket->get_id(), $objTicket->getPatientId(),
            $objTicket->getDepartmentId(), $objTicket->getEnumPatientType(), $objTicket->getEnumExaminationType());
        $objPayment->persist();
        
        return $objPayment->get_id();
   }

    public function getPaymentInfoByPatient($patientId) {
        $sql    =   array(
            'cols'  =>  "$this->_key",
            'from'  =>  "$this->_table t",
            'where' =>  "t.patient_id = '$patientId'"
        );
        $ds =   new Datasource_sql();
        $ds->setup($this->_db, $sql);

        return $ds->toArray();
    }
}
?>
